//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ----------------------------- Ebrahim Foulaadvand, 29 July 2012 ------------------------------------- // // // // The programme "DrivenDampedOscillator" evaluates the temporal evolution of a sinusoidally driven // // damped harmonic oscillator by two algorithms Euler-Richardson (ER) and 2nd order Runge-Kutta (RK2). // // It also computes the oscillator amplitude dependence on the driving force frequency. // // // // // // // //////////////////////////////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include #include #include using namespace std; double a (double &t,double &x,double &v); // a is the accelration function double tau=0.01,T=400,ac,k=9.,m=1.,w0=sqrt(k/m),x0=0.,v0=0.,gamma=0.5,beta=gamma/(2*m),omega,delta, A0=1.,E,kx1,kx2,lx1,lx2,tmid,xmid,vmid,Aanalytic,Anumeric=0.,omegamax=4.,delomega=0.1; // x0=initial position, v0=initial velocity, tau=timestep, T=simulation time, E=total energy, // m=oscillator mass, k=spring constant, gamma=damping coefficient, omega=frequency of external force. // A0=F0/m where F0 is the driving force amplitude, Aanalytic=analytical amplitude, Anumeric=computed // amplitude, omegamax=the maximum frequncy for which the amplitude is comuputed, delomega=omega increment. int N=(T/tau), Nomega=int(omegamax/delomega); //N=number of time steps main() { vector v(N+1,0),x(N+1,0),Acomp(Nomega,0),Aanal(Nomega,0); // arrays x and v store oscillator position and velocity. Array Acomp stores the amplitude for a given omega. ofstream xER("xER.plt"); // output file of oscillator poistion (ER algorithm). ofstream vER("vER.plt"); // output file of oscillator velocity (ER algorithm). ofstream xRK2("xRK2.plt"); // output file of oscillator poistion (RK2 algorithm). ofstream vRK2("vRK2.plt"); // output file of oscillator velocity (RK2 algorithm). ofstream Energy("Energy.plt"); ofstream phase("phase-space.plt"); ofstream Ampcomp("Acomp-w gamma=0.5.plt"); // output file of computed oscillator amplitude vs omega. ofstream Ampanal("Aanal-w gamma=0.5.plt"); // output file of analytic oscillator amplitude vs omega. for(int s=0;s(10/beta) && t*tau<(10/beta) + 2*2*(M_PI/omega) && x[t]>0 && x[t+1]>x[t]) Anumeric=x[t+1]; }//end t Ampcomp<(10/beta) && t*tau<(10/beta) + 2*(M_PI/omega) && x[t]>0 && x[t+1]>x[t]) Anumeric=x[t+1]; }//end t //Ampcomp<